package com.ctsi.question.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.question.dto.PersonSelectDTO;
import com.ctsi.question.dto.ReportLeaderDTO;
import com.ctsi.question.entity.ReportLeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * ReportLeader Service 类
 * @author   zhangjw
 * @description  ReportLeader
 * @created	 Mon Sep 02 10:36:08 CST 2019
 */

@Service
public class ReportLeaderService {
	private static final Logger logger=LoggerFactory.getLogger(ReportLeaderService.class);

	@Autowired
	private MybatisGenericDao dao;
	/**
	 * 添加
	 */
	private void insertReportLeader(ReportLeader reportLeader){
		if(UtilValidate.isEmpty(reportLeader.getId()))
			reportLeader.setId(UUIDGenerator.getId());
		logger.debug("insert {}",reportLeader);
		dao.insert(reportLeader);
	}
	/**
	 * 动态更新
	 */
	public int updateReportLeaderByPKSelective(ReportLeader reportLeader){
		logger.debug("updateByPrimaryKeySelective {}",reportLeader);
		return dao.update(ReportLeader.class.getName()+".updateByPrimaryKeySelective", reportLeader);
	}
	/**
	 * 更新
	 */
	public int updateReportLeader(ReportLeader reportLeader){
		logger.debug("update {}",reportLeader);
		return dao.update(reportLeader);
	}


	public ReportLeader editReportLeaderById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (ReportLeader)dao.queryForObject(ReportLeader.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 ReportLeader对象
	 */
	public ReportLeader findReportLeaderById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(ReportLeader.class, id);
	}
	/**
	 * 删除ReportLeader对象
	 */
	public int removeReportLeader(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(ReportLeader.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}

	/**
	 * 详细页查询
	 */
	public ReportLeader findByDetail(String id){
		return (ReportLeader)dao.queryForObject(ReportLeader.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<ReportLeader> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(ReportLeader.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/**
	 * 多条件查询结果不分页
	 */
	public List<ReportLeader> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<ReportLeader>)dao.queryForList(ReportLeader.class.getName()+".selectByMap",map);
	}

	/**
	 * 分页获取领导关系
	 * @param personNameLike
	 * @param parentId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<ReportLeaderDTO> findReportLeaderDTOPage(String personNameLike,String parentId,int start,int pageSize){
		Map<String,Object> params = new HashMap<>();
		params.put("personNameLike",personNameLike);
		params.put("parentId",parentId);
		return dao.queryPage(ReportLeader.class.getName()+".findReportLeader",params,start,pageSize);
	}

	/**
	 * 添加领导关系
	 * @param reportLeader
	 */
	public void insert(ReportLeader reportLeader){
		Integer count = (Integer) dao.queryForObject(ReportLeader.class.getName()+".ifExit",new HashMap(){{
			this.put("id","root");
		}});
		if (count == 0) {
			ReportLeader temp = new ReportLeader();
			temp.setId("root");
			temp.setLft(1);
			temp.setRgt(2);
			this.insertReportLeader(temp);
		}
		// 查询目标节点左值
		int left = (int) dao.queryForObject(ReportLeader.class.getName()+".findLftOfDest",reportLeader.getParentId());
		dao.update(ReportLeader.class.getName()+".updateLeaderLft",left);
		dao.update(ReportLeader.class.getName()+".updateLeaderRgt",left);
		reportLeader.setLft(left + 1);
		reportLeader.setRgt(left+2);
		this.insertReportLeader(reportLeader);
	}

	/**
	 * 验证人员是否存在
	 * @param personId
	 * @param parentId
	 * @return
	 */
	public boolean personExit(String personId,String parentId){
		Map<String,Object> params = new HashMap<>();
		params.put("personId",personId);
		params.put("parentId",parentId);
		Integer count = (Integer) dao.queryForObject(ReportLeader.class.getName()+".personExits",params);
		return count != 0;
	}

	/**
	 * 领导授权模块，人员选择
	 * @param personNameLike
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<PersonSelectDTO> findPersonSelect(String personNameLike,Integer start,Integer pageSize){
		Map<String,Object> params = new HashMap<>();
		params.put("personNameLike",personNameLike);
		return this.dao.queryPage(ReportLeader.class.getName()+".personSelectQuery",params,start,pageSize);
	}

	/**
	 * 删除
	 * @param ids
	 */
	public void delMuti(Set<String> ids){
		ids.forEach(i -> this.del(i));
	}

	/**
	 * 通过ID 递归删除
	 * @param id
	 */
	public void del(String id){
		ReportLeader leader = this.findReportLeaderById(id);
		if (leader != null) {
			String parentId = leader.getParentId();
			if (UtilValidate.isNotEmpty(parentId)) {
				del(parentId);
			}
			this.removeReportLeader(id);
		}
	}

}
